<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        img {
            vertical-align: top;
        }

        .swiper {
            width: 1226px;
            margin: 50px auto;
            position: relative;
        }

        .swiper-wrapper {
            width: 100%;
        }

        .swiper-wrapper .slider-item {
            width: 100%;
            display: none;
        }

        .swiper-wrapper .show {
            display: block;
        }

        .swiper-wrapper .slider-item img {
            width: 100%;
        }

        .navbar {
            position: absolute;
            right: 50px;
            bottom: 20px;
            display: flex;

        }

        .navbar li {
            width: 10px;
            height: 10px;
            margin: 0 5px;
            border-radius: 50%;
            border: 1px solid #999999;
            background-color: rgba(0, 0, 0, 0.2);
        }

        .navbar .active {
            background-color: #fff;
        }
    </style>
</head>

<body>
    <!-- .swiper>.swiper-wrapper>.slider-item*4>img[src=../images/2.webp] -->
    <div class="swiper">
        <div class="swiper-wrapper">
            <div class="slider-item show"><img src="../images/1.jpg" alt=""></div>
            <div class="slider-item"><img src="../images/2.webp" alt=""></div>
            <div class="slider-item"><img src="../images/3.webp" alt=""></div>
            <div class="slider-item"><img src="../images/4.webp" alt=""></div>
        </div>
        <ul class="navbar">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>
<script>

    // 轮播的功能
    // 1. 点击切换
    // 2. 自动切换
    // 3. 移入暂停 移出启动

    var swiper = document.getElementsByClassName("swiper")[0];
    var swiperWrapper = document.getElementsByClassName("swiper-wrapper")[0];
    var swiperSlideItems = document.getElementsByClassName("slider-item");
    var navBar = document.getElementsByClassName("nav")[0];
    var dotList = document.getElementsByTagName("li");

    var timer = null; // 全局变量 记录轮播的编号(方便后续清除)
    var index = 0;   // 全局变量 记录轮播的下标(方便下次轮播切换)

    autoPlay();

    // 点击切换
    for (var i = 0; i < dotList.length; i++) {
        var dot = dotList[i];
        dot.onclick = function () {
            // var idx = Array.from(dotList).indexOf(this);
            // index = idx;

            index = Array.from(dotList).indexOf(this);  // 记录点击的位置 下次轮播从当前位置开始

            step();

        }
    }

    // 移入暂停 移出移动
    swiper.onmouseenter = function () {
        clearInterval(timer);
    }
    swiper.onmouseleave = function () {
        autoPlay();
    }





    // 自动轮播
    // 1. 每隔2s切换下标 =>  对应下标的点变为活跃状态 对应下标slideItem显示
    // 2. 四张图片,下标最大值 4-1   => 超出之后从头开始 index= 0
    function autoPlay() {
        clearInterval(timer);
        timer = setInterval(function () {
            index++;  // 2s切换下一张

            if (index > dotList.length - 1) { // index > 3
                index = 0;
            }

            step();

        }, 3000)
    }



    function step() {
        // 先清除dotList所有点的样式 slideItem全都隐藏  
        for (var j = 0; j < dotList.length; j++) {
            dotList[j].className = ""
            swiperSlideItems[j].classList.remove("show");
        }

        // 对应下标的点变为活跃状态 对应下标slideItem显示
        dotList[index].className = "active";
        swiperSlideItems[index].classList.add("show");
    }





</script>

</html>